<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-28 16:23
 * Modified Time: 2019-06-28 16:23
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;

use Flower\Core\Repository\Contracts\CriteriaInterface;
use Flower\Core\Repository\Contracts\RepositoryInterface;

class findOrderAppBarberCriteria implements CriteriaInterface
{
    protected $where;

    public function __construct($storeId, $status, $barberId = null, $keyword = null, $mobile = null, $memberId = null, $isOffline = false, $date = null)
    {
        $this->where = [
            'status' => $status,
            'barber_id' => $barberId,
            'store_id' => $storeId,
            'mobile' => $mobile,
            'member_id' => $memberId,
            'keyword' => $keyword,
            'is_offline' => $isOffline,
            'date' => $date,
        ];
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $map['order.store_id'] = $this->where['store_id'];
        $map['order.status'] = isset($this->where['status']) ? $this->where['status'] : 1;

        empty($this->where['barber_id']) ? null : $map['order.barber_id'] = $this->where['barber_id'];

        empty($this->where['member_id']) ? null : $map['order.member_id'] = $this->where['member_id'];

        $isShowComment = ($this->where['status'] == config('constant.orderStatus.done')) ? true : false;
        $isOffline = $this->where['is_offline'];
        $setDate = isset($this->where['date']) ? $this->where['date'] : date('Y-m-d');


//		$year = 0;
//		$month = 0;
//		if ($isSetDate) {
//			$date = explode('-', $this->where['yearMonth']);
//			$year = $date[0];
//			$month = $date[1];
//		}
        // 线下订单显示提成 不是线下订单不显示提成
//        $list = $this->where['is_offline'] ? $model->select('order.*', 'royalty.amount as royalty') : $model->select('order.*');
        $list = $model->select('order.*');
        empty($this->where['keyword']) ? null : $list = $list->leftJoin('appointment', 'appointment.id', 'order.appointment_id')
            ->leftJoin('member', 'member.id', 'appointment.member_id')
            ->orWhere('appointment.mobile', 'like', '%' . $this->where['keyword'] . '%')
            ->orWhere('appointment.nick', 'like', '%' . $this->where['keyword'] . '%')
            ->orWhere('member.mobile', 'like', '%' . $this->where['keyword'] . '%')
            ->orWhere('member.name', 'like', '%' . $this->where['keyword'] . '%')
            ->orWhere('order.order_no', 'like', '%' . $this->where['keyword'] . '%')
            ->orWhere('order.id', '=', $this->where['keyword']);
//	        ->
//        when($hasBarber, function ($v) use ($map) {
//            $v->
//			leftJoin('royalty', function ($join) use ($map) {
////                $join->on('royalty.item_id', '=', 'order.id')->where(['royalty.type' => 1, 'royalty.user_id' => $map['order.barber_id'], 'royalty.store_id' => $map['order.store_id']]);
//	            $join->on('royalty.item_id','=','order.id')->where(['royalty.type'=>1]);
//            })
//        })
	    $list->with(
                [
                    'member:id,name,face,mobile',
                    'user:id,nick,portrait',
                    'assistant:id,nick,portrait',
                    'appointment:id,appointment_time,mobile,nick,remark',
                    'store:id,name,address,doorplate,phone',
                    'memberCard:id,name,mobile'
                ]
            )
	        ->when($map['order.status'] == config('constant.orderStatus.done'),function ($v){
	        	$v->with(['royalty'=>function($q){
			        $q->select([
				        'item_id',
				        'amount',
			        ])->where(['royalty.type'=>1]);
		        }]);
	        })
            ->with(['items' => function ($query) {
                $query
                    ->select([
                        'id',
                        'order_id',
                        'service_id',
                        'price',
                        'real_price',
                        'count'])
                    ->with(['service' => function ($query) {
                        $query->with(['category' => function ($query) {
                            $query->select(['id', 'fullname', 'resource'])->with(['picture']);
                        }]);
                    }]);
            }])

            ->when($isOffline, function ($v) use ($setDate) {
                $v->where('order.source', '=', config('constant.appointment.store'))->whereDate('order.created_at', $setDate);
            })

            ->when($isShowComment, function ($query) {
                $query->with([
                    'comment' => function ($v) {
                        $v->select(['id', 'order_id', 'member_id', 'comment', 'is_goods', 'created_at'])->with('resource');
                    }
                ]);
            })
            ->where($map);
        return $map['order.status'] == config('constant.orderStatus.done') ? $list->orderBy('order.finish_time', 'desc') : $list->orderBy('order.id', 'desc');
    }

}
